package com.simon.hse.activity.safe.saferisk;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.SafetyCardGetListInput;
import com.simon.hse.http.entity.SafetyCardGetListOutput;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 告知确认卡查询
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_work_permit_query)
public class SafeAndRiskQueryActivity extends BaseActivity {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.tv_function_1)
    TextView mTv_function_1;
    @ViewInject(R.id.ll_function_1)
    LinearLayout mLl_function_1;

    @ViewInject(R.id.tv_function_2)
    TextView mTv_function_2;
    @ViewInject(R.id.ll_function_2)
    LinearLayout mLl_function_2;

    @ViewInject(R.id.tv_function_3)
    TextView mTv_function_3;
    @ViewInject(R.id.ll_function_3)
    LinearLayout mLl_function_3;

    @ViewInject(R.id.tv_function_4)
    TextView mTv_function_4;
    @ViewInject(R.id.ll_function_4)
    LinearLayout mLl_function_4;

    @ViewInject(R.id.common_swiperefreshlayout)
    SmartRefreshLayout mSmartRefresh;

    @ViewInject(R.id.common_recyclerview)
    RecyclerView mRecyclerView;

    @ViewInject(R.id.iv_common_right)
    ImageView mIv_common_right;


    private int currentPage = 1, totalPage = 1;
    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<SafetyCardGetListOutput.DataBean.PageDataBean> mDataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mTv_common_title.setText("告知确认卡查询");
        mIv_common_right.setImageResource(R.mipmap.tab_add_icon);

        initRecyclerView();
        initData();
    }


    @Event(value = {R.id.iv_common_left, R.id.iv_common_right, R.id.ll_function_1, R.id.ll_function_2, R.id.ll_function_3, R.id.ll_function_4})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.iv_common_right:
                startActivity(new Intent(SafeAndRiskQueryActivity.this, ApplySafeAndRiskActivity.class));
                break;
            case R.id.ll_function_1:
                initSafePop(mLl_function_1, mTv_function_1, MyApplication.getInstance().mSafeQureyStateList);
                break;
            case R.id.ll_function_2:
                initSafePop(mLl_function_2, mTv_function_2, MyApplication.getInstance().mSafePersonInChargeList);
                break;
            case R.id.ll_function_3:
                publishSafeDate(mTv_function_3);
                break;
            case R.id.ll_function_4:
                publishSafeDate(mTv_function_4);
                break;
        }
    }

    private void initRecyclerView() {
        mTv_function_1.setHint("状态");
        mTv_function_2.setHint("作业负责人");
        mTv_function_3.setHint("申请时间");
        mTv_function_4.setHint("起止时间");
        mSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (currentPage * 10 < totalPage) {
                    currentPage++;
                    initData();
                } else {
                    mSmartRefresh.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mDataBeanList.clear();
                initData();
            }
        });

        LinearLayoutManager llm = new LinearLayoutManager(SafeAndRiskQueryActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(SafeAndRiskQueryActivity.this, R.layout.layout_item_safe_and_risk, mDataBeanList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<String>() {
            @Override
            public void setItemData(CommonViewHolder holder, String dataBean) {
                // TextView tv_danger_stop_day = holder.getView(R.id.tv_danger_stop_day);

            }
        });

        mCommonEmptyAdapter.setOnItemClickListener(new CommonEmptyAdapter.OnItemClickListener() {
            @Override
            public void setOnItemClickListener(View view, int position) {
                Intent intent = new Intent(SafeAndRiskQueryActivity.this, SafeAndRiskDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("from", "query");
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    private void initData() {
        SafetyCardGetListInput safetyCardGetListInput = new SafetyCardGetListInput();
//        safetyCardGetListInput.setFactory();
        safetyCardGetListInput.setPagingQueryModel("");
        Call<String> call = HttpManager.getInstance().requestService().SafetyCard_GetList(HttpManager.getInstance().requestBody(safetyCardGetListInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                SafetyCardGetListOutput safetyCardGetListOutput = new Gson().fromJson(result, SafetyCardGetListOutput.class);
                if (safetyCardGetListOutput.getResult().equals("Success")) {
                    totalPage = safetyCardGetListOutput.getData().getRowCount();
                    List<SafetyCardGetListOutput.DataBean.PageDataBean> pageDataBeanList = safetyCardGetListOutput.getData().getPageData();
                    mDataBeanList.addAll(pageDataBeanList);
                    mCommonEmptyAdapter.notifyDataSetChanged();
                    mSmartRefresh.finishRefresh();
                    mSmartRefresh.finishLoadMore();
                } else {
                    ToastUtils.getInstance().showShortToastBottom(safetyCardGetListOutput.getMessage());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }
}
